package top.rainf.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.rainf.domain.vo.Post;
import top.rainf.mapper.AreaMapper;
import top.rainf.mapper.BlockMapper;
import top.rainf.mapper.PostMapper;
import top.rainf.mapper.UserBaseInfoMapper;
import top.rainf.service.IPostService;
import top.rainf.support.StringMatcher;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description Post服务的实现类
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class PostService implements IPostService {

    @Autowired
    PostMapper postMapper;
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    BlockMapper blockMapper;
    @Autowired
    UserBaseInfoMapper userBaseInfoMapper;


    @Override
    public String postOne(int userId, int blockId, int clazzId, String title, String content) {
        int suc = postMapper.insertNewPost(userId,blockId,clazzId,title,content);
        if(suc != 1){
            return "发帖失败,可能是分类、版块不存在，或者作者不存在";
        }

        //发帖成功,添加数量在触发器
        return "";
    }

    @Override
    public List<Post> getPostList() {
        return postMapper.selectAll();
    }



    @Override
    public Post getOneById(int id) {
        return postMapper.selectById(id);
    }

    @Override
    public boolean deleteOneById(int id) {
        return postMapper.deleteById(id) == 1;
    }

    @Override
    public boolean deleteAll(List<Integer> idList) {
        return postMapper.deleteByIds(idList) == idList.size();
    }

    @Override
    public boolean updateById(Post post) {
        return postMapper.updateById(post) == 1;
    }

    @Override
    public List<Post> getHotPost(int num) {
        return postMapper.selectHottestPostLimit(num);
    }

    @Override
    public List<Post> getAllPostListPagedByBlockId(int id, int numPerPage, int nowPage) {
        return postMapper.selectAllByBlockIdPaged(id,(nowPage-1) * numPerPage,numPerPage);
    }

    @Override
    public int getTotalCount() {
        return postMapper.selectTotalCount();
    }

    @Override
    public int getTotalCountInBlock(int blockId) {
        return postMapper.selectTotalCountInBlock(blockId);
    }

    @Override
    public List<Post> selectByPaged(Map paraMap, Post params) {
        List<Post> postList = postMapper.selectByPaged(paraMap);
        System.out.println(postList);
        return postList;
    }

    @Override
    public int getTotalCountQuery(Map paraMap) {
        return postMapper.selectCountPaged(paraMap);
    }

    @Override
    public List<Post> getUserSimplePostListPaged(int userId, int nowPage, int numPerPage) {
        int start = (nowPage - 1) * numPerPage;
        return postMapper.selectUserSimplePostListPaged(userId,start,numPerPage);
    }

    @Override
    public int getTotalCountOfUser(int userId) {
        return postMapper.selectCountOfUserPost(userId);
    }
}
